<%@ page import="admin.Connectmysql" %>
<%@ page import="java.sql.ResultSet" %>
<%@ page import="java.sql.SQLException" %><%--
  Created by IntelliJ IDEA.
  User: ASUS
  Date: 2025/6/25
  Time: 0:14
  To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%!
    String escapeJson(String value) {
        if (value == null) return "";
        return value.replace("\\", "\\\\")
                .replace("\"", "\\\"")
                .replace("\n", "\\n")
                .replace("\r", "\\r")
                .replace("\t", "\\t");
    }
%>
<%

    Connectmysql cmysql = new Connectmysql(
            "jdbc:mysql://localhost:3306/exam1",
            "root",
            "root",
            "com.mysql.cj.jdbc.Driver");
    try {
        if(cmysql.open()){

            ResultSet rst1= cmysql.executeSql(
                    "select count(*) from teacher;",
                    new Object[]{}
            );
            rst1.next();
            String teacher = rst1.getString(1);
            ResultSet rst2= cmysql.executeSql(
                    "select count(*) from student;",
                    new Object[]{}
            );
            rst2.next();
            String student = rst2.getString(1);
            ResultSet rst3= cmysql.executeSql(
                    "select count(*) from admin;",
                    new Object[]{}
            );
            ResultSet rst4= cmysql.executeSql(
                    "select count(*) from class;",
                    new Object[]{}
            );
            rst4.next();
            String classes = rst4.getString(1);
            ResultSet rst5= cmysql.executeSql(
                    "select count(*) from course;",
                    new Object[]{}
            );
            rst5.next();
            String course = rst5.getString(1);
            rst3.next();
            String admin = rst3.getString(1);
            //Gson gson = new Gson();
            StringBuilder json = new StringBuilder("[");

            for (int i = 0; i < 1; i++) {

                json.append("{");
                json.append("\"teacher\":\"").append(escapeJson(teacher)).append("\",");
                json.append("\"classes\":\"").append(escapeJson(classes)).append("\",");
                json.append("\"course\":\"").append(escapeJson(course)).append("\",");
                json.append("\"student\":\"").append(escapeJson(student)).append("\",");
                json.append("\"admin\":\"").append(escapeJson(admin)).append("\"");
                json.append("}");
                if (i < 1 - 1) {
                    json.append(",");
                }
            }
            json.append("]");
            // 输出JSON
            response.setContentType("application/json");
            response.setCharacterEncoding("UTF-8");
            response.getWriter().write(json.toString());
            rst1.close();rst2.close();rst3.close();
        }
    } catch (ClassNotFoundException f) {
        f.printStackTrace();
    } catch (SQLException throwables) {
        throwables.printStackTrace();
    }
%>
